package com.usian.controller;

import com.usian.feign.ItemServiceFeign;
import com.usian.pojo.TbItem;
import com.usian.pojo.TbItemCat;
import com.usian.pojo.TbItemParam;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("backend/")
public class ItemController {
@Autowired
private ItemServiceFeign itemServiceFeign;

    @RequestMapping("item/selectItemInfo")
    public Result  selectItemInfo(Long itemId) {
        TbItem tbItem = itemServiceFeign.selectItemInfo(itemId);
        if (tbItem != null) {
            return Result.ok(tbItem);
        }
        return Result.error("错错错");
    }
    @RequestMapping("item/selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1")
                                                Integer page, @RequestParam(defaultValue = "5") Integer rows) {

        PageResult pageResult = itemServiceFeign.selectTbItemAllByPage(page, rows);
        if (pageResult.getResult() != null && pageResult.getResult().size() > 0) {
            return Result.ok(pageResult);
        }
        return Result.error("查无结果");
    }
    //分页查
    @RequestMapping("itemCategory/selectItemCategoryByParentId")
    public Result selectItemCategoryByParentId(@RequestParam(defaultValue = "0") Long id){
       List<TbItemCat> list = itemServiceFeign.selectItemCategoryByParentId(id);
       if (list != null ){
           return Result.ok(list);
       }
        return Result.error("错误");
    }
    ///backend/itemParam/selectItemParamByItemCatId'
    //查底部
    @RequestMapping("itemParam/selectItemParamByItemCatId/{itemCatId}")
        public Result selectItemParamByItemCatId(@PathVariable Long itemCatId){
        TbItemParam tbItemParam = itemServiceFeign.selectItemParamByItemCatId(itemCatId);
        if (tbItemParam != null){
          return Result.ok(tbItemParam);
        }
        return Result.error("错");
    }
    //添加
    ///backend/item/insertTbItem
    @RequestMapping("item/insertTbItem")
    public Result insertTbItem(TbItem tbItem, String desc
    ,String itemParams){
      Integer a = itemServiceFeign.insertTbItem(tbItem,desc,itemParams);
      if (a==3){
          return Result.ok();
      }
        return Result.error("添加错误");
    }
//    //删除
    @RequestMapping("item/deleteItemById")
    public Result deleteItemById(Long itemId){
      Integer delId = itemServiceFeign.deleteItemById(itemId);
      if (delId==1){
          return  Result.ok();
      }
        return  Result.error("删除错误");
    }
//    @PostMapping("item/deleteItemById")
//    public Result deleteItemById(Long itemId){
//    Integer de = itemServiceFeign.delete(itemId);
//      if (de!=null){
//          return  Result.ok();
//      }
//        return  Result.error("删除错误");
//    }
    //回显/backend/item/preUpdateItem
    @PostMapping("item/preUpdateItem")
    public Result preUpdateItem(Long itemId){
        Map<String,Object> map = itemServiceFeign.findById(itemId);
        if (map!=null){
            return Result.ok(map);
        }
        return Result.error("回显失败");
    }
    //修改
    @RequestMapping("item/updateTbItem")
    public Result update(TbItem tbItem, String desc
            ,String itemParams){
        Integer a = itemServiceFeign.update(tbItem,desc,itemParams);
        if (a==3){
            return Result.ok();
        }
        return Result.error("添加错误");
    }
    //规格展示
    @RequestMapping("itemParam/selectItemParamAll")
    public Result selectItemParamAll(){
      PageResult page =  itemServiceFeign.selectItemParamAll();
      if (page!=null){
          return  Result.ok(page);
      }
      return   Result.error("查询错误");
    }
    //backend/itemParam/insertItemParam  添加规格参数
    @RequestMapping("itemParam/insertItemParam")
    public Result insertItemParam(TbItemParam itemParam){
        Integer a = itemServiceFeign.insertItemParam(itemParam);
        if (a!=null){
            return Result.ok();
        }
        return Result.error("规格添加错误");
    }
    // Request URL: http://localhost:8080/product_api/backend/itemParam/deleteItemParamById?id=28
    @GetMapping("itemParam/deleteItemParamById")
    public Result deleteItemParamById(Long id){
        Integer a = itemServiceFeign.deleteItemParamById(id);
        if (a!=null){
            return Result.ok();
        }
        return Result.error("删除失败");
    }
}
